/*
 * Copyright 2017 hv0912
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vimhe.ezimp2.config.bean;

import com.vimhe.ezimp2.Constants;
import com.vimhe.ezimp2.config.exception.ConfigException;
import com.vimhe.ezimp2.util.ConvertUtils;
import com.vimhe.ezimp2.util.StringUtils;

/**
 * 工作表配置对象
 *
 * @author Vimhe
 */
public final class SheetConfig {

    /**
     * 工作表索引
     */
    private Integer index;

    /**
     * 对应数据库中的表名
     */
    private String table;

    /**
     * 是否在数据库中自动创建“table”中指定的表
     */
    private Boolean create;

    /**
     * 行配置对象
     */
    private RowConfig rowConfig;

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        if (index < 1 || index > 255) {
            throw new ConfigException("配置项【sheet > index】的值【" + index + "】是无效的，该值必须是1-255间的整数");
        }

        this.index = index;
    }

    public void setIndex(String index) {
        if (StringUtils.isBlank(index)) {
            throw new ConfigException("必须指定配置项【sheet > index】的值");
        }

        try {
            Integer iIndex = ConvertUtils.convert(index, Integer.class);
            this.setIndex(iIndex);
        } catch (Exception e) {
            throw new ConfigException("配置项【sheet > index】的值【" + index + "】是无效的，该值只能是整数");
        }
    }

    public String getTable() {
        return table;
    }

    public void setTable(String table) {
        this.table = table;
    }

    public Boolean getCreate() {
        return create;
    }

    public void setCreate(Boolean create) {
        this.create = create;
    }

    public void setCreate(String create) {
        if (StringUtils.isBlank(create)) {
            create = Constants.BOOLEAN_VALUE_FALSE;
        }

        try {
            Boolean bCreate = ConvertUtils.convert(create, Boolean.class);
            this.setCreate(bCreate);
        } catch (Exception e) {
            throw new ConfigException("配置项【sheet > create】的值【" + create + "】是无效的，该值只能是“true”或“false”");
        }
    }

    public RowConfig getRowConfig() {
        return rowConfig;
    }

    public void setRowConfig(RowConfig rowConfig) {
        this.rowConfig = rowConfig;
    }

}
